package com.medialog.svnTest;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

public class SvnConnection {
	
/*	@Value("#{svnKit['userName']}") private String userName;
	@Value("#{svnKit['password']}") private String password;
	@Value("#{svnKit['url']}") private String url;*/
	
	private final String userName = "sunnykor";
	private final String password = "sunnykor";
	private final String url = "svn://172.26.140.27";
	
	private static SvnConnection svn;//volatile
	private SVNRepository repo;
	private SvnConnection(){
		this.repo = this.getSVNRepository();
	}
	
	private SVNRepository getSVNRepository(){
		SVNRepository repo = null;
		
		try {
			SVNRepositoryFactoryImpl.setup(); //Using protocol- svn://
			//FSRepositoryFactory.setup(); //Using protocol- file://			
			//DAVRepositoryFactory.setup(); //Using protocol- http:// or https://
			
			//repo = SVNRepositoryFactoryImpl.create(SVNURL.parseURIEncoded(this.url));
			//repo = FSRepositoryFactory.create(SVNURL.parseURIEncoded(this.url));
			//repo = DAVRepositoryFactory.create(SVNURL.parseURIEncoded(this.url));		
			
			repo = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(this.url));//Abstract Factory...
			ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(this.userName, this.password);
			repo.setAuthenticationManager(authManager);
			
		} catch (Exception e) { //e.printStackTrace();
		}
		
		return repo;
	}
	
	public static SvnConnection getInstance(){
		if(SvnConnection.svn == null){
			synchronized(SvnConnection.class){
				if(SvnConnection.svn == null){
					try { SvnConnection.svn = new SvnConnection();
					} catch (Exception e) { //e.printStackTrace();
					}
				}
			}
		}
		
		return SvnConnection.svn;
	}
	
	/*public void SvnCloseSession(){
		SvnConnection.svn.SvnCloseSession();
	}*/
	
	public List<SVNDirEntry> getDirEntries(String url){
		List<SVNDirEntry> dirEntries = new ArrayList<SVNDirEntry>();
		
		try { this.repo.getDir(url, -1, false, dirEntries);
		
		} catch (Exception e) { e.printStackTrace();
		}
		
		return dirEntries;
	}
	
	public List<SvnDomain> getTreeviewEntries(String url){
		List<SvnDomain> list = new ArrayList<SvnDomain>();
		this.getList(url, list);
		
		int liSize = list.size();
		List<String> tmpList = new ArrayList<String>();
		for(int i = 0; i < liSize; i++){
			tmpList.add(list.get(i).getFnServletUrl());
		}
		
		int ord = 0;
		for(int i = 0; i < liSize; i++){
			SvnDomain domain = list.get(i);
			String parentPath = domain.getFnServletUrl().replace("/"+domain.getFnAlias(), "");
			
			domain.setFnId("" + i);
			domain.setFnParentId((tmpList.contains(parentPath)) ? "" + tmpList.indexOf(parentPath) : "0");
			domain.setFnOrder((i > 0 && list.get(i - 1).getFnParentId().equals(domain.getFnParentId())) ? ++ord : 0);
		}
		
		return list;
	}
	
	private void getList(String url, List<SvnDomain> list){
		List<SVNDirEntry> entries = this.getDirEntries(url);
		Iterator<SVNDirEntry> it = entries.iterator();
		
		while(it.hasNext()){
			SVNDirEntry e = it.next();
			
			if(e.getKind() == SVNNodeKind.DIR){
				String name = e.getName();
				String path = url + "/" + name;

				SvnDomain domain = new SvnDomain();
				domain.setFnUseYN("Y");
				domain.setFnCollapse(false);
				domain.setFnAlias(name);
				domain.setFnServletUrl(path);
				list.add(domain);
				
				this.getList(path, list);
			}
		}
	}
	
}
